﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HelloWPF.DependencyProperty
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public int MyProperty { get; set; }

        //1.声明
        public static readonly System.Windows.DependencyProperty ValueProperty;

        /// <summary>
        /// CLR属性包装器
        /// GetValue、SetValue来源于DependencyObject，你这个对象就必须继承自DependencyObject
        /// 包装器的作用是以“实例属性”的形式向外界暴露依赖属性，
        /// 这样依赖属性才能成为数据源的一个Path
        /// </summary>
        public int Value 
        {
            get { return (int)GetValue(ValueProperty); }
            set
            {
                SetValue(ValueProperty, value);
            }
        }

        /// <summary>
        /// 静态构造函数，CLR只执行一次
        /// </summary>
        static MainWindow()
        {
            ValueProperty = System.Windows.DependencyProperty.Register("Value", typeof(string), typeof(MainWindow));
        }

        public MainWindow()
        {
            //普通属性
            MyProperty = 123;

            //不可用
            //ValueProperty = 123;

            //这种才可用，太麻烦
            SetValue(ValueProperty,123);

            InitializeComponent();
        }
    }
}
